#include "stdafx.h"
#include "Time.h"
#include <mmsystem.h>

TimeTicks::TimeTicks(uint64 tick)
	: m_ticks(tick)
{

}

bool TimeTicks::operator<(TimeTicks& others)
{
	return m_ticks < others.m_ticks;
}

bool TimeTicks::operator>(TimeTicks& others)
{
	return m_ticks > others.m_ticks;
}

bool TimeTicks::operator<=(TimeTicks& others)
{
	return m_ticks < others.m_ticks;
}

bool TimeTicks::operator>=(TimeTicks& others)
{
	return m_ticks >= others.m_ticks;
}

TimeTicks TimeTicks::operator+(uint64 tick)
{
	return TimeTicks(m_ticks + tick);
}

TimeTicks TimeTicks::operator-(uint64 tick)
{
	return TimeTicks(m_ticks - tick);
}

TimeTicks TimeTicks::operator+(TimeTicks& other)
{
	return TimeTicks(m_ticks + other.m_ticks);
}

TimeTicks TimeTicks::operator-(TimeTicks& other)
{
	return TimeTicks(m_ticks - other.m_ticks);
}

uint64 TimeTicks::getTick()
{
	return m_ticks;
}


uint32 Time::m_tickLastUsed = 0;
bool Time::high_res_time_ = false;
Time::Time()
{
}


Time::~Time()
{
}

bool Time::highResTime(bool open)
{
	MMRESULT result = TIMERR_NOERROR;
	if (open)
	{
		result = timeBeginPeriod(1);
		if (result == TIMERR_NOERROR)
		{
			high_res_time_ = true;
		}
	} 
	else
	{
		result = timeEndPeriod(1);
		if (result == TIMERR_NOERROR)
		{
			high_res_time_ = false;
		}
	}

	return result == TIMERR_NOERROR;
}

bool Time::isHighResTime()
{
	return high_res_time_;
}

TimeTicks Time::now()
{
	uint64 retTime = 0;
	uint32 nowTime = timeGetTime();
	if (nowTime < m_tickLastUsed)
	{
		retTime = nowTime + 0x100000000I64; // 49.7days
	}
	else
	{
		retTime = nowTime;
	}

	m_tickLastUsed = nowTime;

	return TimeTicks(retTime);
}
